import {defineStore} from "pinia";
import {post} from "@/request";
import {get} from "../request";
import {useIndexStore} from "./main";

export const useUserStore = defineStore("User", {
    state: () => ({
        // 存放通知
        inform: [],
        // 本地存放的用户
        local_user: [],
        // 当前登录用户对象
        user: {
            account:'2755877533',
            password:'zsj051227',
        },
        // 对方用户
        to_user: {},
        // 添加用户时对象
        add_user_obj: {},
        // 添加群聊
        add_group_obj: {},
        // 消息对象
        message: {},
        // 消息列表
        message_list: [],
        // 消息数组
        show_message_list: [],
        // 搜索关键字
        search_key: "",
        // 在好友列表搜索的关键字
        inner_search_key: "",
        // 搜索后的群聊列表
        search_groups: [],
        // 搜索后用户列表
        search_users: [],
        indexStore: null,
        // 存放inform数组
        inform_list: [],
    }),
    getters: {
        show_message() {

        }
    },

    actions: {
        // 初始化socket
        initIndexStore() {
            this.indexStore = useIndexStore();
        },
        // 登录
        async on_login() {
            try {
                const data = await post("/users/login", this.user);
                return [data.code === 200, data.data];
            } catch (e) {
                console.error(e);
                return false;
            }
        },
        // 注册
        async on_register() {
            try {
                const data = await post("/users/add", this.user);
                console.log(data.code);
                return data.code === 200;
            } catch (e) {
                console.error(e);
                return false;
            }
        },
        set_user(data) {
            Object.assign(this.user, data);
        },
        set_to_user(data) {
            Object.assign(this.to_user, data);
        },
        filter(items, searchKey) {
            if (!searchKey) return items; // 如果搜索关键字为空，则返回原数组

            // 构建正则表达式
            const regex = new RegExp(`.*${searchKey}.*`, "i");
            // 进行筛选
            let arr = []
            items.forEach(item => {
                item.members.forEach(member => {
                    if (regex.test(member.name) || regex.test(member.account)) {
                        arr.push({...member})
                    }
                })
            })
            return arr
        },
        // 搜索
        async on_search(type = "linkman") {
            if (type === "linkman") {
                this.search_users = this.filter(this.user['groups'], this.inner_search_key)
            } else {
                const users = await get("/users", {search_key: this.search_key});
                const groups = await get("/group", {search_key: this.search_key});

                this.search_users = users.data
                this.search_groups = groups.data
            }
        },
        set_inform(data) {
            Object.assign(this.inform, data);
        },
        agree_add_user(data, type) {
            this.indexStore.socket.emit("agree_add_user", data);
        },
        un_agree_add_user(data, type) {
            this.indexStore.socket.emit("un_agree_add_user", data);
        },
        async get_show_message_list() {
            const message = await get('/show_message', {userId: this.user._id})
            this.show_message_list = message.data
        },
        // 获取当前与用户对话的消息列表
        async set_message_list(user_id, to_user_id) {
            const message = await get('/message', {user_id, to_user_id})
            this.message_list = message.data
        },
        async set_inform_list() {
            const inform = await get('/inform')
            this.inform = inform.data
        },
    },
});
